package hz.mobileproject.pda.toolsmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.caption.netmonitorlibrary.netStateLib.NetUtils;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.app.MainApp;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.toolsmanager.adapter.ToolsMainGVAdapter;
import hz.mobileproject.pda.toolsmanager.bean.Item_ToolsMain;
import hz.mobileproject.pda.toolsmanager.bean.request.RQ_GetSysSet;
import hz.mobileproject.pda.toolsmanager.bean.response.RP_ToolSysSet;
import hz.mobileproject.pda.ui.base.BaseNetActivity;
import hz.mobileproject.pda.ui.view.CustomListDialog;
import hz.mobileproject.pda.ui.view.LoadingDialog;
import hz.mobileproject.pda.utils.HttpRqUtils;


@ContentView(R.layout.activity_tools_main)
public class ToolsMainActivity extends BaseNetActivity {
    @ViewInject(R.id.gv_tools_manager)
    private GridView gv_tools_manager;
    @ViewInject(R.id.tv_look_call_time)
    private TextView tv_look_call_time;

    private Gson mGson;
    //配置信息
    private RP_ToolSysSet mToolSystemSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGson = new Gson();
        List<Item_ToolsMain> mainItemList = new ArrayList<>();
        mainItemList.add(new Item_ToolsMain("工具发放", R.mipmap.tools_fafang));
        mainItemList.add(new Item_ToolsMain("工具回收", R.mipmap.tools_huishou));
        mainItemList.add(new Item_ToolsMain("工具报废", R.mipmap.tools_baofei));
        mainItemList.add(new Item_ToolsMain("增加库存", R.mipmap.tools_zengjia));
        mainItemList.add(new Item_ToolsMain("工具外借", R.mipmap.tools_waijie));
        mainItemList.add(new Item_ToolsMain("工具归还", R.mipmap.tools_guihuan));
        mainItemList.add(new Item_ToolsMain("工位变更", R.mipmap.tools_gongwei));
        mainItemList.add(new Item_ToolsMain("小件管理", R.mipmap.tools_guanli));
        mainItemList.add(new Item_ToolsMain("清点记录", R.mipmap.tools_jilu));
        ToolsMainGVAdapter adapter = new ToolsMainGVAdapter(this, mainItemList);
        gv_tools_manager.setAdapter(adapter);
        gv_tools_manager.setOnItemClickListener(onItemClickListener);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取配置信息
        getToolSystemSetFromSer();
    }

    /**
     * 主页菜单点击事件
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if (mToolSystemSet == null) {
                ToastUtils.showLong("未获取到配置信息，请重新尝试");
                getToolSystemSetFromSer();
                return;
            }
            Intent intent = new Intent();
            intent.putExtra("key", mToolSystemSet);
            switch (i) {
                case 0://工具发放
                    intent.setClass(ToolsMainActivity.this, ToolsGrantActivity.class);
                    startActivity(intent);
                    break;
                case 1://工具回收
                    intent.setClass(ToolsMainActivity.this, ToolsRetrieveActivity.class);
                    startActivity(intent);
                    break;
                case 2://工具报废
                    intent.setClass(ToolsMainActivity.this, ToolsScrapActivity.class);
                    startActivity(intent);
                    break;
                case 3://增加库存
                    intent.setClass(ToolsMainActivity.this, AddReportoryActivity.class);
                    startActivity(intent);
                    break;
                case 4://工具外借
                    if (mToolSystemSet.getBorrow_tool() == 1) {  //1为允许外接
                        intent.setClass(ToolsMainActivity.this, WaiJieToolsActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtils.showLong("不允许外借，请通知技术人员");
                    }
                    break;
                case 5://工具归还
                    if (mToolSystemSet.getBorrow_tool() == 1) {  //1为允许外接和归还
                        intent.setClass(ToolsMainActivity.this, ToolRecyclerActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtils.showLong("不允许归还，请通知技术人员");
                    }
                    break;
                case 6://工位变更
                    if (mToolSystemSet.getGive_object() == 3) {
                        intent.setClass(ToolsMainActivity.this, ToolChangeActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtils.showLong("请开启工位和使用人模式");
                    }
                    break;
                case 7://小件管理
                    intent.setClass(ToolsMainActivity.this, ToolManagerActivity.class);
                    startActivity(intent);
                    break;
                case 8://清点记录
                    startActivity(intent.setClass(ToolsMainActivity.this, ToolCallJiLuActivity.class));
                    break;
            }
        }
    };


    @Event(value = {R.id.tv_start_inventory,R.id.tv_look_call_time})
    private void viewOnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_start_inventory:
                if (mToolSystemSet == null) {
                    ToastUtils.showLong("未获取到配置信息，请重新尝试");
                    getToolSystemSetFromSer();
                    return;
                }
                //获取是否在时间段
                isCalllingTimeFromSer();
                break;
            case R.id.tv_look_call_time:
                ArrayList<String> stringList = new ArrayList<>();
                for(RP_ToolSysSet.TimeData timeData : mToolSystemSet.getTime_data()){
                    stringList.add("开始时间："+timeData.getStart_time()+"\t结束时间："+timeData.getEnd_time());
                }
                CustomListDialog customListDialog = new CustomListDialog(ToolsMainActivity.this,stringList);
                customListDialog.show();
                break;
        }
    }

    /**
     * 从后台调取接口获取配置信息
     */
    private void getToolSystemSetFromSer() {
        RQ_GetSysSet getSysSet = new RQ_GetSysSet();
        getSysSet.setCus_number(MainApp.getInstance().getLoginUser().getCus_number() + "");
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_TOOL_GET_SYS_SET, getSysSet, new CustomNetCallBack() {
            @Override
            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                LogUtils.e(data);
                mToolSystemSet = mGson.fromJson(data, RP_ToolSysSet.class);
                if (mToolSystemSet == null) {
                    ToastUtils.showLong("系统配置信息格式错误或者为空");
                    return;
                }
                //TODO 平板测试
                // mToolSystemSet.setSweep_type(2);
                if (mToolSystemSet.getTime_data() != null && mToolSystemSet.getTime_data().size() > 0) {
                    tv_look_call_time.setVisibility(View.VISIBLE);
                }else{
                    tv_look_call_time.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onErrorFromSer(int rtnCode, String rtnMsg) {
                ToastUtils.showLong("获取系统配置信息失败");
            }

            @Override
            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
                ToastUtils.showLong("获取系统配置信息失败");
            }
        });
    }

    private void isCalllingTimeFromSer() {
        final LoadingDialog loadingDialog = new LoadingDialog(this);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setIsCanBack(true).setMessage("清点时间验证中").show();
        HttpRqUtils.httpGet(Constance.BASE_URL + Constance.URL_TOOL_GET_CLEAR_TIME, new CustomNetCallBack() {
            @Override
            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                if(!loadingDialog.isShowing()){
                    return;
                }
                loadingDialog.dismiss();
                Intent intent = new Intent(ToolsMainActivity.this, ToolsCallV1Activity.class);
                intent.putExtra("key", mToolSystemSet);
                startActivity(intent);
            }

            @Override
            public void onErrorFromSer(int rtnCode, String rtnMsg) {
                loadingDialog.dismiss();
                ToastUtils.showLong("清点时间验证失败(" + rtnCode + "," + rtnMsg + ")");
            }

            @Override
            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
                loadingDialog.dismiss();
                ToastUtils.showLong("清点时间验证失败");
                LogUtils.e(ex.toString());
            }
        });
    }


    /**
     * 网络连接状态
     *
     * @param type 网络状态
     */
    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        LogUtils.e("网络状态：" + type.toString());
    }

    /**
     * 网络断开
     */
    @Override
    protected void onNetworkDisConnected() {
        LogUtils.e("网络断开");
    }

}
